package com.kurumi.medical.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.kurumi.medical.param.Report;
import com.kurumi.medical.param.ReportItem;
import com.kurumi.medical.param.ReportRoot;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class Analysis_Xml {
	
//	public static void main(String[] args) throws Exception{
//		FileReader reader = new FileReader(new File("D://xml/5027645.xml"));
//		String str = "";
//		char[] c = new char[1024];
//		int length = 0;
//		while((length = reader.read(c))!=-1) {
//			str+=new String(c, 0, length);
//		}
//		
//		getCheckReportOrderDetailMsg2(str);
//        // System.out.println(ReportRoot);
//		 //System.out.println(ReportRoot.getReport().getReportItems());
//	}

	/**
	 * 检验报告单详细信息
	 * @param xmlStr
	 * @return
	 */
	public static Result getCheckReportOrderDetailMsg(String xmlStr) {
		try {
			Document document = DocumentHelper.parseText(xmlStr);
			Element rootElement = document.getRootElement();
			Element getReportResult = rootElement.element("Body").element("GetReportResponse").element("GetReportResult");
			 XStream xStream = new XStream(new DomDriver());
			 xStream.setClassLoader(ReportRoot.class.getClassLoader());
			 //xml文档中节点映射到对应的类
			 xStream.alias("reportRoot", ReportRoot.class); 
			 xStream.alias("report", Report.class);
			 xStream.alias("reportItem", ReportItem.class);
			 //集合转换
			 xStream.addImplicitCollection(Report.class, "reportItems");
			 ReportRoot obj = (ReportRoot) xStream.fromXML(getReportResult.getTextTrim());
			 
			 return new Result(true, "解析xml成功!", obj, 1);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "解析xml出错!", null, -1);
		}
		
	}
	
	
	/**
	 * 检验报告单详细信息
	 * @param xmlStr
	 * @return
	 */
	public static Result getCheckReportOrderDetailMsg2(String xmlStr) {
		ReportRoot reportRoot = new ReportRoot();
		Report report = new Report();
		try {
			Document document = DocumentHelper.parseText(xmlStr);
			Element rootElement = document.getRootElement();
			Element getReportResult = rootElement.element("Body").element("GetReportResponse").element("GetReportResult");
			String xmlResult = getReportResult.getTextTrim();
			
			document = DocumentHelper.parseText(xmlResult);
			
			rootElement = document.getRootElement();
			Element codeElement = rootElement.element("code");
			String code = codeElement.getTextTrim();
			reportRoot.setCode(StringUtils.isEmpty(code)?null:code);
			Element msgElement = rootElement.element("msg");
			String msg = msgElement.getTextTrim();
			reportRoot.setMsg(StringUtils.isEmpty(msg)?null:msg);
			Element reportElement = rootElement.element("report");
			if (null != reportElement) {
				Element nameElement = reportElement.element("name");
				String name = nameElement.getTextTrim();
				report.setName(StringUtils.isEmpty(name)?null:name);
				
				List<Element> reportItemsElement = reportElement.elements("reportItem");
				if(CollectionUtils.isEmpty(reportItemsElement)){
					
				}
				for (Element reportItemElement : reportItemsElement) {
					Element reportNameItemElement = reportItemElement.element("name");
					String reportNameItem = reportNameItemElement.getTextTrim();
					System.out.println(reportNameItem);
				}
			
			}
			
			 return new Result(true, "解析xml成功!", null, 1);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "解析xml出错!", null, -1);
		}
		
	}
	
	/**
	 * 检验报告单id信息
	 * @param xmlStr
	 * @return
	 */
	public static Result getBasicDataIdList(String xmlStr) {
		List<Integer> idList = null;
		String code = "-1";
				
		try {
			Document document = DocumentHelper.parseText(xmlStr);
			Element rootElement = document.getRootElement();
			Element getIdsByMRNResultElement = rootElement.element("Body").element("GetIdsByMRNResponse").element("GetIdsByMRNResult");
			String xmlResult = getIdsByMRNResultElement.getTextTrim();
			
			document = DocumentHelper.parseText(xmlResult);
			rootElement = document.getRootElement();
			Element codeElement = rootElement.element("code");
			code = codeElement.getTextTrim();
			
			Element reportsElement = rootElement.element("reports");
			
			if (null != reportsElement) {
				List<Element> reportListElement = reportsElement.elements("report");
				idList = new ArrayList<>();
				
				if (reportListElement != null && reportListElement.size() > 0) {
					for (int i = 0; i < reportListElement.size(); i++) {
						Element idElement = reportListElement.get(i).element("id");
						String id = idElement.getTextTrim();
						idList.add(Integer.parseInt(id));
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "解析基础数据xml异常", null, Integer.parseInt(code));
		}
		return new Result(true, "解析成功!", idList, Integer.parseInt(code));
	}
	
	
	
	
	
	
}



